package hu.tortavar2.client.activity;

import java.util.List;
import java.util.logging.Logger;

import hu.tortavar2.client.ClientFactory;
import hu.tortavar2.client.SiteService;
import hu.tortavar2.client.SiteServiceAsync;
import hu.tortavar2.client.place.ProductListPlace;
import hu.tortavar2.client.view.layout.ProductListView;
import hu.tortavar2.client.view.layout.ProductListView.Presenter;
import hu.tortavar2.client.view.layout.ProductListViewImpl;
import hu.tortavar2.shared.Product;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class ProductListActivity extends AbstractActivity implements Presenter {

	private final Logger log = Logger.getLogger(ProductListActivity.class.getName());
	SiteServiceAsync siteService = GWT.create(SiteService.class);
	
	private final ProductListPlace place;
	private final ProductListView view;
	private final PlaceController placeController;
	
	public ProductListActivity(ProductListPlace place, ClientFactory clientFactory) {
		this.place = place;
		view = new ProductListViewImpl();
		placeController = clientFactory.getPlaceController();
	}
	
	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		// TODO Auto-generated method stub
		view.setPresenter(this);
		panel.setWidget(view.asWidget());
		refreshProductList();
	}

	@Override
	public void onProductSelected(String product) {
		// TODO Auto-generated method stub

	}

	@Override
	public void goTo(Place place) {
		// TODO Auto-generated method stub
		placeController.goTo(place);
	}

	@Override
	public void refreshProductList() {
		// TODO Auto-generated method stub
		view.setLoaderImage();
		siteService.getProducts(place.getProductType(), new AsyncCallback<List<Product>>() {
			
			@Override
			public void onSuccess(List<Product> result) {
				// TODO Auto-generated method stub
				view.setProductList(result);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}
}